<template>
  <div class="info_wrapper">
    <h3>基本信息</h3>
    <el-divider></el-divider>
    <div class="avatar">
      <el-upload
        ref="upload"
        :on-success="uploadSuccess"
        :show-file-list="false"
        action="http://localhost:9999/common/upload?flag=6"
        list-type="picture-card"
      >
        <img :src="memberInfo.memberAvatar" class="avatar" />
        <el-button type="primary">更换头像</el-button>
      </el-upload>
    </div>
    <el-form
      class="memberInfo"
      ref="memberInfoForm"
      :model="memberInfo"
      label-width="70px"
    >
      <el-form-item prop="nickName" label="昵称">
        <el-input v-model="memberInfo.nickName" clearable></el-input>
      </el-form-item>
      <el-form-item prop="memberSex" label="性别">
        <el-radio v-model="memberInfo.memberSex" :label="false">男</el-radio>
        <el-radio v-model="memberInfo.memberSex" :label="true">女</el-radio>
      </el-form-item>
      <el-form-item prop="memberBirthday" label="生日">
        <el-date-picker
          v-model="memberInfo.memberBirthday"
          type="date"
          placeholder="选择日期"
          :clearable="false"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="memberProfile" label="个人简介">
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="memberInfo.memberProfile"
          maxlength="30"
          show-word-limit
          resize="none"
        ></el-input>
      </el-form-item>
      <el-button type="primary" @click="saveInfo">保存</el-button>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      memberInfo: {},
    };
  },
  created() {
    if (sessionStorage.getItem("memberId") != null) {
      this.getUserInfo();
    }
  },
  methods: {
    async getUserInfo() {
      let id = sessionStorage.getItem("memberId");
      const { data: res } = await this.$http.get(
        "member/member/getMemberById",
        { params: { id: id } }
      );
      if (res.code != 200) {
        return this.$message.error(res.message);
      } else {
        this.memberInfo = res.data;
        sessionStorage.setItem("avatar", this.memberInfo.memberAvatar);
        sessionStorage.setItem("memberName", this.memberInfo.nickName);
      }
    },
    uploadSuccess(res, file) {
      console.log("uploadres", res);
      console.log("file", file);
      this.$message.success(res.message);
      this.memberInfo.memberAvatar = file.response.data.url;
      this.saveInfo();
    },
    async saveInfo() {
      console.log(typeof this.memberInfo.memberBirthday);
      console.log(this.memberInfo.memberBirthday);
      if (
        typeof this.memberInfo.memberBirthday != "string" &&
        this.memberInfo.memberBirthday != null
      ) {
        this.memberInfo.memberBirthday = this.formatDate(
          this.memberInfo.memberBirthday,
          1
        );
      }
      const { data: res } = await this.$http.post(
        "member/member/editMember",
        this.memberInfo
      );
      if (res.code != 200) {
        return this.$message.error("保存失败");
      } else {
        this.$message.success("保存成功");
        location.reload();
      }
    },
  },
};
</script>
<style lang="less" scoped>
.avatar {
  float: left;
  /deep/.el-upload--picture-card {
    width: 260px;
    height: 260px;
  }
  img {
    width: 100%;
    height: 100%;
  }
  .el-button {
    position: relative;
    top: -31px;
  }
}
.memberInfo {
  float: left;
  margin-left: 70px;
}
</style>